package com.yls.except.util.imgfile;

import com.aliyun.openservices.ClientConfiguration;
import com.aliyun.openservices.oss.OSSClient;
import com.aliyun.openservices.oss.model.ObjectMetadata;
import com.aliyun.openservices.oss.model.PutObjectResult;
import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

public class OssUpload {
	private static final String accessKeyId = "LTAITBYiI1ouqZTI";
	private static final String accessKeySecret = "xQlxKNd2kmBm9nIPo11qrKJqrv8AwA";
	private static final String bucketName = "xiaowanyi";
	private static final String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
//	private static final String accessKeyId = "HdKCOjoG5xoLPLon";
//	private static final String accessKeySecret = "VSsagGNkYafUYGO5SRUVjx5dsuA9ly";
//	private static final String bucketName = "except";
//	private static final String endpoint = "http://oss-cn-shanghai.aliyuncs.com";
	/**
	 * 上传到本地
	 * @param file
	 * @param getImagePath
	 * @param fileNewName
	 * @throws IOException
     */
	public static void upload (MultipartFile file, String getImagePath, String fileNewName) throws IOException {
//		int w = 0;
//		int h = 0;
		File image = new File(getImagePath);
		if (!image.exists() && !image.isDirectory()) {
			image.mkdirs();
		}
		BufferedImage srcBufferImage = ImageIO.read(file.getInputStream());
		ScaleImage scaleImage = ScaleImage.getInstance();
		int weight = srcBufferImage.getWidth();
		int height = srcBufferImage.getHeight();
		BufferedImage scaledImage = scaleImage.imageZoomOut(srcBufferImage, weight, height);
		FileOutputStream out = new FileOutputStream(getImagePath + "/"
				+ fileNewName);
		ImageIO.write(scaledImage, "jpeg", out); 
	}


    /**
	 *  上传到阿里云
	 * @param filename 文件名称
	 * @param in 文件流
	 * @param contenttype 文件类型
     * @return
     */
	// 上传图片到AliOss
	public  static String UpImgToAlisOss(String filename, InputStream in, String contenttype) {
		try {
			// 初始化OSSClient
			// String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
			OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
			ClientConfiguration conf = new ClientConfiguration();
			conf.setConnectionTimeout(30000);  //超时时间
			conf.setMaxErrorRetry(3);  //最大错误请求次数
			// 创建上传Object的Metadata
			ObjectMetadata meta = new ObjectMetadata();
			// 必须设置ContentLength
			meta.setContentLength(in.available());
			meta.setContentType(contenttype);
//			meta.setContentType("image/jpeg");
//			meta.setContentType("text/hmtl");
			// 上传Object.
			PutObjectResult result = client.putObject(bucketName, filename, in, meta);
			System.out.println(result.getETag());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "/" + filename;
	}

	public static void main(String [] args) {
//		try {
//			InputStream in = new FileInputStream("D:\\a.webp");   //换成你本地的图片试试
//			UpImgToAlisOss("data/img/a.jpg", in, "image/jpeg");
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		}
		try {
			InputStream in = new FileInputStream("D:\\a.webp");
			FileUtils.copyInputStreamToFile(in, new File("D:\\", "a.jpg"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
